<?php

//$dg = new sfDatagrid2WidgetDoctrineDatagrid(array(
//	'model' => 'Member',
//	'caption' => 'Mitglieder',
//	'collapsable' => true,
//	'columns' => array(
//		'Nachname' => '##last_name##',
//		'Vorname' => '##first_name##',
//		'Nachname, Vorname' => '##last_name##, ##first_name##'),
//	'coll' => $members,
//	'iterate_css_classes' => array('odd', 'even', 'third')
//));


class sfDatagrid2WidgetDatagrid extends sfWidget {
	
	/**
	 * Configures the current widget.
	 *
	 * Available options:
	 *
	 * * caption:       		caption text of data grid
	 * * collapsable			true or false
	 * * rows_visible			number of rows visible at once
	 * * rows_per_page			numer or false to deactivate paging
	 * * columns				array
	 * * iterate_css_classes	array
	 * * batch_actions			false or array
	 *
	 * @param array $options     An array of options
	 * @param array $attributes  An array of default HTML attributes
	 *
	 * @see sfWidget
	 */
	protected function configure($options = array(), $attributes = array()) {
		$this->addOption ( 'caption');
		$this->addOption ( 'collapsable', false );
		$this->addOption ( 'rows_visible', 10 );
		$this->addOption ( 'rows_per_page', false );
		$this->addOption('iterate_css_classes', array());
		$this->addOption('batch_actions', false);
		
		$this->addRequiredOption( 'columns' );
	}
	
	public function render($name, $value = null, $attributes = array(), $errors = array()) {
		$html = '<table border="1" name="'.$name.'" class="datagrid">';
		$html.= $this->renderTableHeader();
		$html.= $this->renderTableFooter();
		$html.= $this->renderTableBody();
		$html.= '</table>';
		return $html;
	}
	
	protected function renderTableHeader() {
		$outerHeader = '';
		$innerHeader = '';
		if($this->hasCaption()) {
			$outerHeader.= $this->renderCaption();
		}
		$columns = $this->getOption('columns');
		foreach($columns as $label => $binding) {
			$innerHeader.= $this->renderContentTag('th', $label);
		}
		$outerHeader.= $this->renderContentTag('thead', $innerHeader);
		return $outerHeader;
	}
	
	protected function renderCaption() {
		$options = $this->renderContentTag('span', $this->renderCaptionOptions(), array('class' => 'options'));
		return $this->renderContentTag('caption', $options.$this->getOption('caption'));
	}
	
	protected function renderCaptionOptions() {
		$options = '';
		if($this->getOption('collapsable')) {
			$options = 'Collaps';
		}
		return $options;
	}
	
	protected function renderTableFooter() {
		
	}
	
	protected function renderTableBody() {
		
	}
	
	protected function renderBodyRow($cells, $num) {
		$class = null;
		if(is_array($this->getOption('iterate_css_classes'))) {
			$classes = $this->getOption('iterate_css_classes');
			$count = count($classes);
			$class = $classes[($num%$count)];
		}
		return $this->renderContentTag('tr', $cells, array('class' => $class));
	}
	
	public function hasCaption() {
		$caption = $this->getOption ( 'caption' );
		return ($caption !== null && $caption != false) || $this->getOption('collapsable');
	}
}